package sanrenxing.dayEnd.backRepertoryTotal.dao;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import sanrenxing.dayEnd.backRepertoryTotal.domain.CashReceivable;

import java.util.List;
import java.util.Map;

/**
 * @desrciption 现金应收应付  dao层
 * @auth：yidu 李小锋
 * @date:2021/3/13 0013 - 16:22
 * @version:1:0
 */
@Repository
public interface MoneyitiesDao {
    /**
     * 新增一行数据
     * @param cashReceivable 对象
     * @return 0、增加失败 1、增加成功
     */
    @Insert("insert into cashReceivable values(seqCashReceivable.nextval,#{businDate},#{caId},#{businType},#{capFlow},#{money},#{fundId},#{states})")
    public int add(CashReceivable cashReceivable);

    /**
     * 根据现金应收应付编号删除现金应收应付信息
     * @param moneyId 现金应收应付编号
     * @return 1：删除成功；0：删除失败
     */
    @Delete("delete from cashReceivable where Id=#{moneyId}")
    public int delete(@Param("moneyId") int moneyId);
    /**
     * 修改一行数据
     * @param cashReceivable 对象
     * @return 0、修改失败 1、修改成功
     */
    @Update("update cashReceivable set businDate=#{businDate},caId=#{caId},businType=#{businType},capFlow=#{capFlow},money=#{money},fundId=#{fundId},taStockId=#{taStockId},states=#{states}" +
            "where moneyId=#{moneyId}")
    public int update(CashReceivable cashReceivable);
    /**
     * 查询所有的订单数据
     * @return 返回一个订单集合
     */
    @Select("select * from cashReceivable")
    @Results(id = "cashMap",value = {
            @Result(id = true,column = "Id",property = "Id"),
            @Result(column = "businDate",property = "businDate"),
            @Result(column = "caId",property = "caId"),
            @Result(column = "businType",property = "businType"),
            @Result(column = "capFlow",property = "capFlow"),
            @Result(column = "money",property = "money"),
            @Result(column = "fundId",property = "fundId"),
            @Result(column = "taStockId",property = "taStockId"),
            @Result(column = "states",property = "states")
    })
    public List<CashReceivable> findAll();
    /**
     * 分页查询
     * @param start  开始行号
     * @param rows   每页行数
     * @param condition 查询条件
     * @return 集合
     */
    @ResultMap(value = "cashMap")
    @Select("<script>"+
            "select * from " +
            "(select rownum  rn,cashReceivable.* from cashReceivable " +
            "<where> #{rows}>rownum </where>)"+
            "<where>"+
            /*"<if test='rn!=0'>"+*/
            " ${condition}"+
            /*"</if>"+*/
            " and rn>=#{start}"+
            "</where>"+
            "</script>")
    public List<CashReceivable> findByPage(@Param("start") int start, @Param("rows") int rows, @Param("condition") String condition);
    /**
     * 按条件统计出记录行数
     * @param condition 条件
     * @return 记录行数
     */
    @Select("select count(*) from cashReceivable where 1=1 ${condition}")
    public int count(String condition);

    /**
     * 获取需要反冲销数据的id
     */
    @Select("select id,ids from  (select cr.id as id,crs.id as ids,rownum rn from cashReceivable cr join cashReceivable crs on \n" +
            "cr.businType=crs.businType where cr.id<>crs.id and cr.money=crs.money and cr.capflow<>crs.capflow and cr.capFlow=1 and crs.capFlow=-1)\n" +
            "where rn between 0 and 1")
    public Map<String,Object> washDelete();

}
